<?php
/*
 * @copyright   Leyun internet Technology(Shanghai)Co.,Ltd
 * @license     http://www.dzzoffice.com/licenses/license.txt
 * @package     DzzOffice
 * @link        http://www.dzzoffice.com
 * @author      zyx(zyx@dzz.cc)
 */
if(!defined('IN_DZZ') || !defined('IN_ADMIN')) {
	exit('Access Denied');
}
include libfile('function/admin');
include libfile('function/organization');
$do=empty($_GET['do'])?'available':trim($_GET['do']);

$page = empty($_GET['page'])?1:intval($_GET['page']);
$perpage=20;
$gets = array(
		'mod'=>'app',
		'op'=>'import',
		'do' => $do,
	);
$theurl = BASESCRIPT."?".url_implode($gets);
$refer=urlencode($theurl.'&page='.$page);

$order='ORDER BY disp';
$start=($page-1)*$perpage;
$apps=array();
if($do=='available') {
	if($count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('app_market')." WHERE  available<1")){
		$apps=DB::fetch_all("SELECT * FROM ".DB::table('app_market')." WHERE  available<1  limit $start,$perpage");
		$multi=multi($count, $perpage, $page, $theurl,'pull-right');
	}
}elseif($do=='notinstall') {
		$identifiers=C::t('app_market')->fetch_all_identifier();
		
		$plugindir = DZZ_ROOT.'./dzz';
		$pluginsdir = dir($plugindir);
		$newplugins = array();
		$list = array();
		while($entry = $pluginsdir->read()) {
			if(!in_array($entry, array('.', '..')) && is_dir($plugindir.'/'.$entry) && !in_array($entry, $identifiers)) {
				$entrydir = DZZ_ROOT.'./dzz/'.$entry;
				$d = dir($entrydir);
				$filemtime = filemtime($entrydir);
				$entrytitle = $entry;
				$entryversion = $entrycopyright = $importtxt = '';
				if(file_exists($entrydir.'/dzz_app_'.$entry.'.xml')) {
					$importtxt = @implode('', file($entrydir.'/dzz_app_'.$entry.'.xml'));
				} 
				if($importtxt) {
					$pluginarray = getimportdata('Dzz! app', 0, 1);
					if(!empty($pluginarray['plugin']['name'])) {
						$pluginarray['plugin']['name'] = dhtmlspecialchars($pluginarray['plugin']['name']);
						$pluginarray['plugin']['version']= dhtmlspecialchars($pluginarray['plugin']['version']);
						$pluginarray['plugin']['copyright'] = dhtmlspecialchars($pluginarray['plugin']['copyright']);
					}
					$list[$entry]=$pluginarray;
				}
			}
		}
			
		
}elseif($do=='upgrade'){
	$sql='';
	if($group){
		$sql=" and `group` = '{$group}'";
	}
	if($count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('app_market')." WHERE 1 $sql")){
		$apps=DB::fetch_all("SELECT * FROM ".DB::table('app_market')." WHERE 1 $sql $order limit $start,$perpage");
		$multi=multi($count, $perpage, $page, $theurl,'pull-right');
	}
}


$list=array();
$grouptitle=array('0'=>'全部','-1'=>'仅游客可用','1'=>'成员可用','2'=>'部门管理员可用','3'=>'仅系统管理员可用');
foreach($apps as $value){
	
	$value['tags']=C::t('app_relative')->fetch_all_by_appid($value['appid']);
	if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
		$value['appico']=$_G['setting']['attachurl'].$value['appico'];
	}
	$value['appurl']=replace_canshu($value['appurl']);
	$value['grouptitle']=$grouptitle[$value['group']];
	$value['department']=getDepartmentByAppid($value['appid']);
	$list[]=$value;
}

include template('import');

?>
